package lxx.zhihudailypaper.activity;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;

import org.json.JSONArray;
import org.json.JSONObject;

import lxx.zhihudailypaper.R;
import lxx.zhihudailypaper.bean.ContentBean;
import lxx.zhihudailypaper.util.StreamUtils;
import lxx.zhihudailypaper.view.MyImageView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ContentActivity extends Activity{
	
	protected static final String tag = "ContentActivity";
	private MyImageView miv_content_photo;
	private WebView wv_content;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			ContentBean bean = (ContentBean)msg.obj;
			miv_content_photo.setImageUrl(bean.getImage());
			wv_content.getSettings().setJavaScriptEnabled(true);
			wv_content.setWebViewClient(new WebViewClient(){
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
						
					view.loadDataWithBaseURL("", url, "text/html", "gbk", "");
					return true;
						
				};
			});
			wv_content.loadDataWithBaseURL(bean.getCss(),bean.getBody(), "text/html", "gbk","");
		};
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_content);
		Intent intent = getIntent();
		String id = intent.getStringExtra("id");
		initUI();
		initData(id);
	}
	
	private void initUI() {
		miv_content_photo = (MyImageView) findViewById(R.id.miv_content_photo);
		wv_content = (WebView) findViewById(R.id.wv_content);
	}

	private void initData(final String id) {
		new Thread(){
			private ContentBean contentBean;

			public void run() {
				try {
					URL url = new URL("http://news-at.zhihu.com/api/4/news/"+id);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET");
					conn.setReadTimeout(10000);
					int rCode = conn.getResponseCode();
					if(rCode == 200){
						InputStream is = conn.getInputStream();
						String rs = StreamUtils.streamToString(is);
						JSONObject json = new JSONObject(rs);
						
						contentBean = new ContentBean();
						contentBean.setTitle(json.getString("title"));
						Log.d(tag, json.getString("title"));
						
						contentBean.setBody(json.getString("body"));
						Log.d(tag, json.getString("body"));
						
						JSONArray jsonArray = json.getJSONArray("images");
						contentBean.setImage(jsonArray.getString(0));
						//contentBean.setImage(json.getString("images"));
						Log.d(tag, json.getString("images"));
						
						JSONArray jsonArray2 = json.getJSONArray("css");
						contentBean.setCss(jsonArray2.getString(0));
						Log.d(tag, json.getString("css"));
						
						//解决乱码
						//contentBean.updateDesc=URLDecoder.decode(obj.getString("updateDesc"), "UTF-8");
						
						Message message = Message.obtain();
						message.obj = contentBean;
						handler.sendMessage(message);
						
						
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			};
		}.start();
	}

	
	
}
